package com.net.wanjian.networkhospitalmanager.fragment.evaluate;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.adapter.SimpleEvaluateAdapter;
import com.net.wanjian.networkhospitalmanager.bean.SearchEvaluateResult;
import com.net.wanjian.networkhospitalmanager.bean.evaluate.SearchEventEvaluateTableResult;
import com.net.wanjian.networkhospitalmanager.bean.evaluate.SubmitEvaluateItemBean;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.fragment.base.BaseFragment;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.ProgressDialogUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.widge.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

/**
 * 简易评价 提交评价页面
 * lidongcai
 */
public class SimpleEvaluateFragment extends BaseFragment {
    public static final String SIMPLE_EVALUATE_FRAGMENT_SIMPLEEVENT = "simple_evaluate_fragment_simpleevent";

    @BindView(R.id.fragment_simple_evaluate_recycler)
    RefreshRecyclerView fragmentSimpleEvaluateRecycler;

    private SimpleEvaluateAdapter simpleEvaluateAdapter;
    private SearchEvaluateResult.DataBean.EventListBean simpleEvent;

    public SimpleEvaluateFragment() {
        // Required empty public constructor
    }

    public static SimpleEvaluateFragment getInstance(SearchEvaluateResult.DataBean.EventListBean simpleEvent) {
        SimpleEvaluateFragment fragment = new SimpleEvaluateFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(SIMPLE_EVALUATE_FRAGMENT_SIMPLEEVENT, simpleEvent);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            simpleEvent = (SearchEvaluateResult.DataBean.EventListBean) getArguments().getSerializable(SIMPLE_EVALUATE_FRAGMENT_SIMPLEEVENT);
        }
    }

    @Override
    protected void initView() {
        fragmentSimpleEvaluateRecycler.setRefreshMode(0);
        fragmentSimpleEvaluateRecycler.setLayoutManager(new LinearLayoutManager(mContext));

        //获取网络数据后创建并设置adapter
        ProgressDialogUtils.showProgressDialog(mContext, "", R.string.loading_text);
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.searchEvaluateTable(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(),
                xmlUtil.getUserIdentityId(), xmlUtil.getUserIdentity(), simpleEvent.getEventID(), simpleEvent.getEventType(), new DisposableObserver<SearchEventEvaluateTableResult>() {
                    @Override
                    public void onNext(SearchEventEvaluateTableResult searchEventEvaluateTableResult) {
                        if (searchEventEvaluateTableResult.getResult() == ErrorCodeReturn.success) {
                            showList(searchEventEvaluateTableResult.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: " );
                        ProgressDialogUtils.dismissProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: " );
                        ProgressDialogUtils.dismissProgressDialog();
                    }
                });
    }

    private void showList(SearchEventEvaluateTableResult.DataBean data) {
//        private String EvaluationType;//评价类型（0：学生对老师，1：学生对事件，2：老师对事件）
//        private String EvaluationTypeName;//评价类型名称 ui上面文字
//        private String IsEvaluated;//是否被评价过（0：未评价过，1：评价过）
//        private EvaluatedUserInfoBean EvaluatedUserInfo;//被评价者（老师）用户信息
//        private String EvaluationTextContentInfo;//文本内容
//        private List<EvaluationItemListBean> EvaluationItemList;//评价项List

        List<SubmitEvaluateItemBean> itemBeanList = new ArrayList<>();
        //封装学生对老师
        for (SearchEventEvaluateTableResult.DataBean.EvaluationTableStudentToTeacherListBean d:data.getEvaluationTableStudentToTeacherList()){
            SubmitEvaluateItemBean bean = new SubmitEvaluateItemBean();
            bean.setEvaluationType(d.getEvaluationType());
            bean.setEvaluationTypeName(d.getEvaluationTypeName());
            bean.setIsEvaluated(Integer.parseInt(d.getIsEvaluated()));
            bean.setEvaluationTextContentInfo(d.getEvaluationTextContentInfo());

            //被评价者信息
            SubmitEvaluateItemBean.EvaluatedUserInfoBean infoBean = new SubmitEvaluateItemBean.EvaluatedUserInfoBean();
            SearchEventEvaluateTableResult.DataBean.EvaluationTableStudentToTeacherListBean.EvaluatedUserInfoBean userInfo = d.getEvaluatedUserInfo();
            infoBean.setUserInfoTrueName(userInfo.getUserInfoTrueName());
            infoBean.setUserInfoID(userInfo.getUserInfoID());
            infoBean.setUserIdentityID(userInfo.getUserIdentityID());
            infoBean.setDepartment(userInfo.getDepartment());
            infoBean.setJobTitle(userInfo.getJobTitle());

            bean.setEvaluatedUserInfo(infoBean);
            //评价项
            List<SubmitEvaluateItemBean.EvaluationItemListBean> itemList = new ArrayList<>();
            for (SearchEventEvaluateTableResult.DataBean.EvaluationTableStudentToTeacherListBean.EvaluationItemListBean item:d.getEvaluationItemList()) {
                SubmitEvaluateItemBean.EvaluationItemListBean itemBean = new SubmitEvaluateItemBean.EvaluationItemListBean();
//                private String EvaluationItemID;//评价项ID
//                private String EventEvaluationItemID;//事件评价项ID
//                private String EvaluationItemName;//评价项名称
//                private String EvaluationItemValueResult;//评价项值
                itemBean.setEvaluationItemID(item.getEvaluationItemID());
                itemBean.setEventEvaluationItemID(item.getEventEvaluationItemID());
                itemBean.setEvaluationItemName(item.getEvaluationItemName());
                if (!item.getEvaluationItemValueResult().equals("")){
                    itemBean.setEvaluationItemValueResult(Integer.parseInt(item.getEvaluationItemValueResult()));
                }else {
                    itemBean.setEvaluationItemValueResult(0);
                }
                itemList.add(itemBean);
            }

            bean.setEvaluationItemList(itemList);
            itemBeanList.add(bean);
        }
        //封装对事件
        for (SearchEventEvaluateTableResult.DataBean.EvaluationTableStudentTeacherToEventListBean d:data.getEvaluationTableStudentTeacherToEventList()){
            SubmitEvaluateItemBean bean = new SubmitEvaluateItemBean();
            bean.setEvaluationType(d.getEvaluationType());
            bean.setEvaluationTypeName(d.getEvaluationTypeName());
            bean.setIsEvaluated(Integer.parseInt(d.getIsEvaluated()));
            bean.setEvaluationTextContentInfo(d.getEvaluationTextContentInfo());

            //被评价者信息
            SubmitEvaluateItemBean.EvaluatedUserInfoBean infoBean = new SubmitEvaluateItemBean.EvaluatedUserInfoBean();
            bean.setEvaluatedUserInfo(infoBean);
            //评价项
            List<SubmitEvaluateItemBean.EvaluationItemListBean> itemList = new ArrayList<>();
            for (SearchEventEvaluateTableResult.DataBean.EvaluationTableStudentTeacherToEventListBean.EvaluationItemListBeanX item:d.getEvaluationItemList()) {
                SubmitEvaluateItemBean.EvaluationItemListBean itemBean = new SubmitEvaluateItemBean.EvaluationItemListBean();
                itemBean.setEvaluationItemID(item.getEvaluationItemID());
                itemBean.setEventEvaluationItemID(item.getEventEvaluationItemID());
                itemBean.setEvaluationItemName(item.getEvaluationItemName());

                if (!item.getEvaluationItemValueResult().equals("")){
                    itemBean.setEvaluationItemValueResult(Integer.parseInt(item.getEvaluationItemValueResult()));
                }else {
                    itemBean.setEvaluationItemValueResult(0);
                }
                itemList.add(itemBean);
            }

            bean.setEvaluationItemList(itemList);

            itemBeanList.add(bean);
        }

        simpleEvaluateAdapter = new SimpleEvaluateAdapter(mContext,simpleEvent);
        fragmentSimpleEvaluateRecycler.setAdapter(simpleEvaluateAdapter);


        //设置adapter
        simpleEvaluateAdapter.setList(itemBeanList);
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_simple_evaluate;
    }

    @Override
    protected void initData() {

    }

}
